package com.huishao.demo.utils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class PwdGenerate {

    static final String algorithm = "AES";
    static final String transformation = "AES";

    static final String key = "";

    public static String generate() throws Exception{
        char[] str = {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                '~', '!', '@', '#', '$', '%', '^', '-', '+'
        };
        StringBuffer stringBuffer = new StringBuffer("");
        while (stringBuffer.length() < 16){
            Random r = new Random();
            stringBuffer.append(str[r.nextInt(45)]);
        }
        return stringBuffer.toString();
    }

    public static String encryption(String pwd) throws Exception{
        Cipher cipher = Cipher.getInstance(transformation);
        SecretKeySpec sks = new SecretKeySpec(pwd.getBytes(), algorithm);
        cipher.init(Cipher.ENCRYPT_MODE,sks);
        byte[] bytes = cipher.doFinal(pwd.getBytes());
        return bytesToHexString(bytes);
    }



    public static String decryp(String pwd)throws Exception{
        Cipher cipher = Cipher.getInstance(transformation);
        SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
        cipher.init(Cipher.DECRYPT_MODE,sks);
        byte [] inputBytes = hexStringToBytes(pwd);
        byte[] bytes = cipher.doFinal(inputBytes);
        return new String(bytes);
    }


    private static byte[] hexStringToBytes(String hexString) {
        if (hexString.length() % 2 != 0) {
            throw new IllegalArgumentException("hexString length not valid");
        }
        int length = hexString.length() / 2;
        byte[] resultBytes = new byte[length];
        for (int index = 0; index < length; index++) {
            String result = hexString.substring(index * 2, index * 2 + 2);
            resultBytes[index] = Integer.valueOf(Integer.parseInt(result, 16)).byteValue();
        }
        return resultBytes;
    }
    private static String bytesToHexString(byte[] sources) {
        if (sources == null) {
            return null;
        }
        StringBuilder stringBuffer = new StringBuilder();
        for (byte source : sources) {
            String result = Integer.toHexString(source& 0xff);
            if (result.length() < 2) {
                result = "0" + result;
            }
            stringBuffer.append(result);
        }
        return stringBuffer.toString();
    }
}
